﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace MotionControl.Core
{
    public class utils
    {
        public static string ObjToStr<T>(T objectToSerialize)
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(T));
            using (MemoryStream stream = new MemoryStream())
            {
                serializer.WriteObject(stream, objectToSerialize);
                byte[] dataBytes = new byte[stream.Length];
                stream.Position = 0;
                stream.Read(dataBytes, 0, (int)stream.Length);
                string dataString = Encoding.UTF8.GetString(dataBytes);
                return dataString;
            }
        }

        public static T ObjFromStr<T>(string stringToDeserialize)
        {
            DataContractSerializer deserializer = new DataContractSerializer(typeof(T));
            using (MemoryStream stream = new MemoryStream())
            {
                return (T)deserializer.ReadObject(stream);
            }
        }
    }
}
